package com.ibm.mall.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ibm.mall.base.BaseAction;
import com.ibm.mall.model.Administrator;
import com.ibm.mall.model.Commodity;
import com.ibm.mall.model.CommodityClass;
import com.ibm.mall.model.PageBean;
import com.ibm.mall.model.User;
import com.ibm.mall.util.MD5Util;
import com.ibm.mall.util.QueryHelper;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class AdministratorAction extends BaseAction<Administrator> {

	private static final long serialVersionUID = 1L;

	private Integer id;
	private String Account;
	private Short administratorType;
	private Map responseJson;
	
	private String username;
	
	private String password;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}



	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAccount() {
		return Account;
	}

	public void setAccount(String account) {
		Account = account;
	}

	public Short getAdministratorType() {
		return administratorType;
	}

	public void setAdministratorType(Short administratorType) {
		this.administratorType = administratorType;
	}

	public Map getResponseJson() {
		return responseJson;
	}

	public void setResponseJson(Map responseJson) {
		this.responseJson = responseJson;
	}

	public String list() {
		return "list";
	}

	public String add() {
		SimpleDateFormat sf = new SimpleDateFormat();
		model.setInsertTime(sf.format(new Date()));
		model.setAdministratorPassword(MD5Util.MD5("1111"));
		model.setLastUpdateTime(sf.format(new Date()));
		model.setStatus((short) 0);
		administratorService.save(model);
		return "add";
	}

	public String edit() {
		SimpleDateFormat sf = new SimpleDateFormat();
		model.setLastUpdateTime(sf.format(new Date()));
	administratorService.updatAdministrator(model);
		return "edit";
	}

	public String delete() {
		Administrator administrator = administratorService
				.getAdministratorById(model.getId());
		administrator.setStatus((short) 1);
		administratorService.updatAdministrator(administrator);
		return "delete";
	}

	public String getData() {
	    Map<String, Object> map = new HashMap<String, Object>();
        QueryHelper queryHelper=null;
        if(Account!=null&&!Account.equals("")){
        	queryHelper=new QueryHelper(Administrator.class, "c")//
			.addCondition("c.status=?", (short)0)//查询出未被删除的
			.addCondition("c.administratorAccount like ?", "%"+Account+"%")
			.addOrderProperty("c.id", true);
        }else{
        	queryHelper=new QueryHelper(Administrator.class, "c")//
			.addCondition("c.status=?", (short)0)//查询出未被删除的
			.addOrderProperty("c.id", true);
        }
		// 分页查询获得pageBean
		PageBean pageBean = administratorService.getPageBean(page, rows,
				queryHelper);
		map.put("rows", pageBean.getRecordList());
		map.put("total", pageBean.getRecordCount());
		this.setResponseJson(map);
		return "getData";
	}
	
	public String loginUI() {
		return "loginUI";
	}
	
	public String login() {
		Administrator administrator=administratorService.getAdministratorByUserAccount(username);
		if(administrator==null){
			ActionContext.getContext().put("msg", "用户名不存在！");
			return "loginUI";
		}else{
			if(administrator.getAdministratorPassword().equals(MD5Util.MD5(password))){
				ActionContext.getContext().getSession().put("admin", administrator);
				return "toIndex";
			}else{
				ActionContext.getContext().put("msg", "密码不正确！");
				return "loginUI";
			}
		}
	}
	
	public String logout() {
		ActionContext.getContext().getSession().remove("admin");
		return "toIndex";
	}
}
